<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Classification extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where  = [];
        $list = db("auth_classification")->where($where)->paginate(5, false, ['query'=>request()->param()]);
        $data = $list->items();
        $this->assign('list',$list);
        $this->assign('data',$data);
        return $this->fetch();
    }
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if(request()->isPost())
        {
            $inputdata = input();
            $data = array();
            $data['classification_name'] = $inputdata['classification_name'];
            $data['sort'] = $inputdata['sort'];
            $classificationModel = model("Classification");
            $rat = $classificationModel->ClassificationAdd($data);
            if($rat){
                $this->success('添加成功',url('Classification/index') );
            }else{
                $this->error('添加失败' );
            }
        }else{
            return $this->fetch();
        }
    }
    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function info(Request $request){
        $id = input( "id" );
        $where = [];
        $where['classification_name_id'] =$id;
        $list = db( "auth_subclassname" )->where( $where )->paginate( 5, false, ['query'=>request()->param()] );
        $data = $list->items();
        $this->assign( "list",$list );
        $this->assign( "data",$data );
        $this->assign( "id",$id );
        return $this->fetch();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function increase()
    {
        $id = input( "id" );
        $list = db( "auth_classification" )->select();
        $this->assign('list',$list);
        $this->assign('id',$id);
        return $this->fetch();
    }

    public function increasesave(Request $request){
        if(request()->isPost())
        {
            $inputdata = input();
            $data = array();
            $data['classification_name_id'] = $inputdata['classification_name'];
            $data['subclass_name'] = $inputdata['subclass_name'];
            $data['img'] = $inputdata['img'];
            $data['sort'] = $inputdata['sort'];
            $list = db('auth_subclassname')->insert($data);
            if($list)
            {
                $this->success('添加成功',url('Classification/index') );
            }
            else
            {
                $this->error('添加失败' );
            }
        }else{
            return $this->fetch();
        }
    }
//子类删除
    public function del($id){
        $id = input('id');
        $res = db('auth_subclassname')->delete($id);
        if($res){
            $this->success('删除成功',url('Classification/index') );
        }else{
            $this->error('删除失败' );
        }
    }
    public function upload(){
        $file =request()->file('file');
        $info=$file->move('public/upload');
        if($info){
            $path="http://erzu.com".'/public/upload/'.$info->getSaveName();
            return json(['code'=>1,'msg'=>'上传成功','data'=>$path]);
        }
    }
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $id = input('id');
        $find  = db( 'auth_classification' )->where( ['id'=>$id] )->find();
        $this->assign( 'find', $find );
        return $this->fetch();
    }
    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $id = intval( input( 'id' ) );
        if( request()->isPost() ) {
            $inputdata  = input();
            $data = array();
            $data['classification_name'] = $inputdata['classification_name'];
            $data['sort'] = $inputdata['sort'];
            $inputdatamodel = model("Classification");
            $res = $inputdatamodel->ClassificationEdit($id,$data);
            //$bool = db( 'auth_classification' )->where( ['id'=>$id] )->update( $data );
            if( $res !== false ){
                $this->success( '编辑成功', url( 'Classification/index' ) );
            }else{
                $this->error( '编辑失败' );
            }
        } else {
            return $this->fetch();
        }
    }
    /**
     * 编辑指定资源
     *modify
     * @param  int  $id
     * @return \think\Response
     */
    public function modify($id){
        $id = input('id');
        $find  = db( 'auth_subclassname' )->where( ['id'=>$id] )->find();
        $this->assign( 'find', $find );

        $where = [];
        $data = db("auth_classification")->where($where)->select();
        $this->assign("data",$data);
        return $this->fetch();
    }
    /**
     * 操作编辑指定资源
     *modify
     * @param  int  $id
     * @return \think\Response
     */
    public function modifyedit(){
            $id = input("id");
            $inputdata  = input();
            $data = array();
            $data['classification_name_id'] = $inputdata['classification_name'];
            $data['subclass_name'] = $inputdata['subclass_name'];
            $data['img'] = $inputdata['img'];
            $data['sort'] = $inputdata['sort'];
//            $inputdatamodel = model("Classification");
//            $res = $inputdatamodel->ClassificationEdit($id,$data);
            $res = db( 'auth_subclassname' )->where( ['id'=>$id] )->update( $data );
            if( $res !== false ) {
                $this->success( '编辑成功', url( 'Classification/index' ) );
            } else {
                $this->error( '编辑失败' );
            }
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function remove($id)
    {
        $id = input('id');
        $res = db('auth_classification')->where('id', $id)->delete();
        if($res){
            $this->success('删除成功','Classification/index');
        }else{
            $this->error('删除失败');
        }
    }
}
